package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.contants.ArticleContants;
import com.heima.model.aritlce.dto.ArticleDto;
import com.heima.model.aritlce.dto.ArticleHomeDto;
import com.heima.model.aritlce.pojo.ApArticle;
import com.heima.model.aritlce.pojo.ApArticleConfig;
import com.heima.model.aritlce.pojo.ApArticleContent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("apArticleService")
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Resource
    private ApArticleMapper apArticleMapper;

    @Resource
    private ApArticleContentMapper apArticleContentMapper;

    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;

    @Override
    public void saveArticle(ArticleDto dto) {
        //1. 判断是新增操作还是修改操作  newsId
        ApArticle apArticle = apArticleMapper.selectOne(Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getNewsId, dto.getNewsId()));
        if (apArticle == null) {
            //3. 如果是新增 , 保存数据到数据库
            //3.1 保存文章基本信息到数据库  ap_article
            apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            apArticle.setLikes(0);
            apArticle.setCollection(0);
            apArticle.setViews(0);
            apArticle.setComment(0);
            apArticle.setCreatedTime(new Date());
            apArticle.setUpdatedTime(new Date());
            apArticleMapper.insert(apArticle);

            //3.2 保存文章配置信息到数据库  ap_article_config
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setEnable(1);
            apArticleConfig.setIsDelete(0);
            apArticleConfig.setIsComment(1);
            apArticleConfig.setIsForward(1);
            apArticleConfig.setCreatedTime(new Date());
            apArticleConfig.setUpdatedTime(new Date());
            apArticleConfigMapper.insert(apArticleConfig);

            //3.1 保存文章内容信息到数据库  ap_article_content
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setCreatedTime(new Date());
            apArticleContent.setUpdatedTime(new Date());
            apArticleContentMapper.insert(apArticleContent);

        } else {
            //4. 如果是修改 , 更新数据到数据库
            //4.1 修改文章基本信息  ap_article
            Long articleId = apArticle.getId();
            BeanUtils.copyProperties(dto, apArticle);
            apArticle.setId(articleId);
            apArticle.setUpdatedTime(new Date());
            apArticleMapper.updateById(apArticle);

            //4.2 修改文章内容信息 ap_article_content
            ApArticleContent apArticleContent = apArticleContentMapper.selectById(articleId);
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setUpdatedTime(new Date());
            apArticleContentMapper.updateById(apArticleContent);
        }
    }

    @Override
    public List<ApArticle> load(ArticleHomeDto dto, short loadType) {
        //1. 校验参数
        //1.1 频道参数处理
        String tag = StringUtils.isEmpty(dto.getTag()) ? ArticleContants.DEFAULT_TAG : dto.getTag();
        //1.2 每页展示数据条数参数处理
        Integer size = dto.getSize() == null ? 10 : dto.getSize();
        size = Math.min(size, 50);
        //1.3 最小时间参数处理
        Date minTime = dto.getMinBehotTime() == null ? new Date() : dto.getMinBehotTime();
        //1.4 最大时间参数处理
        Date maxTime = dto.getMaxBehotTime() == null ? new Date() : dto.getMaxBehotTime();
        //2. 封装查询条件
        Map<String, Object> queryParams = new HashMap<>(5);
        queryParams.put("tag", tag);
        queryParams.put("size", size);
        queryParams.put("minTime", minTime);
        queryParams.put("maxTime", maxTime);
        queryParams.put("loadType", loadType);

        //3. 调用数据访问层完成查询
        List<ApArticle> articleList = apArticleMapper.queryByParams(queryParams);

        //4. 封装数据返回
        return articleList;
    }
}

